package com.i2863.receipt.controller;

import com.i2863.receipt.entity.Site;
import com.i2863.receipt.service.SiteService;
import com.i2863.receipt.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/6/8.
 */
@Controller
@RequestMapping("/site")
public class SiteController {

    @Autowired
    private SiteService siteService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpServletRequest request;

    /**
     * 设置站点图片
     * @return
     */
    @RequestMapping("/setImg")
    public String setImg(ModelMap modelMap){
        HttpSession session = request.getSession();
        Integer siteId = (Integer) session.getAttribute("siteId");
        Site site = siteService.findOne(siteId);
        if(site==null){
            modelMap.put("ImgUrl",null);
        }else{
            modelMap.put("ImgUrl",site.getImgUrl());
        }
        return "site/siteImg";
    }

    @RequestMapping("/saveImg")
    @ResponseBody
    public String saveImg(@RequestParam(value = "ImgUrl",required = true)String ImgUrl){
        HttpSession session = request.getSession();
        Integer siteId = (Integer) session.getAttribute("siteId");
        Site site = siteService.findOne(siteId);
        if(site==null){
            Site entity = new Site();
            entity.setImgUrl(ImgUrl);
            entity.setId(siteId);
            siteService.add(entity);
        }else{
            siteService.setImg(ImgUrl,siteId);
        }
        return ResultJson.successMeg("设置成功");
    }


}
